//
//  WHYPhotoBrowserAnimation.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/12/4.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

// MARK: - 展现动画协议
protocol WHYPhotoBrowserPresentDelegate: NSObjectProtocol {
    /// 参与转场的图片框
    ///
    /// - parameter indexPath: 照片索引
    ///
    /// - returns: 参与转场动画的全新图片框
    func imageViewForPresent(indexPath: NSIndexPath) -> UIImageView

    /// 展现转场图片的起始位置
    ///
    /// - parameter indexPath: 照片索引
    ///
    /// - returns: 起始位置
    func photoBrowserPresentFromRect(indexPath: NSIndexPath) -> CGRect

    /// 展现转场图片动画结束时的屏幕位置
    ///
    /// - parameter indexPath: 照片索引
    ///
    /// - returns: 相对于屏幕的位置
    func photoBrowserPresentToRect(indexPath: NSIndexPath) -> CGRect

}

// MARK: - 解除动画协议
protocol WHYPhotoBrowserDismissDelegate: NSObjectProtocol {

    func imageViewForDismiss () -> UIImageView

    func indexPathForDismiss () -> NSIndexPath

}

// MARK: - 自定义转场动画
class WHYPhotoBrowserAnimator: NSObject, UIViewControllerTransitioningDelegate {
    
    /// 解除转场动画代理
    weak var dismissDelegate: WHYPhotoBrowserDismissDelegate?
    
    /// 展现转场动画代理
    weak var presentDelegate: WHYPhotoBrowserPresentDelegate?
    
    /// 转场动画图片索引
    var indexPath: NSIndexPath?
    
    /// 是否展现
    private var isPresented = false
    
    /// 设置转场动画属性
    ///
    /// - parameter presentDelegate: 展现转场动画代理
    /// - parameter dismissDelegate: 解除转场动画代理
    /// - parameter indexPath:       要显示的图片的索引
    func setupDelegateParameters(presentDelegate: WHYPhotoBrowserPresentDelegate,
        dismissDelegate: WHYPhotoBrowserDismissDelegate,
        indexPath: NSIndexPath) {
            
            self.presentDelegate = presentDelegate
            self.dismissDelegate = dismissDelegate
            self.indexPath = indexPath
            
    }
    
    /// 返回提供 modal 展现的`动画的对象
    ///
    /// - parameter presented:  <#presented description#>
    /// - parameter presenting: <#presenting description#>
    /// - parameter source:     <#source description#>
    ///
    /// - returns: <#return value description#>
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    
    /// 返回提供 dismiss 的`动画对象`
    ///
    /// - parameter dismissed: <#dismissed description#>
    ///
    /// - returns: <#return value description#>
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
    
}

// MARK: - UIViewControllerAnimatedTransitioning
extension WHYPhotoBrowserAnimator: UIViewControllerAnimatedTransitioning {
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 0.8
    }
    
    /// 转场动画实现方法 － 一旦实现此函数，系统的动画方法，将由程序员负责
    ///
    /// - parameter transitionContext: 转场上下文 - 提供转场动画的所有细节
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)
        
    }
    
    /// 展现转场动画
    ///
    /// - parameter transitionContext: 转场上下文
    private func presentAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let presentDelegate  = presentDelegate,
            imageIndexPath = indexPath else {
                return
        }
        
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        toView.alpha = 0.0
        
        transitionContext.containerView()?.addSubview(toView)
        
        let imgView = presentDelegate.imageViewForPresent(imageIndexPath)
        imgView.frame = presentDelegate.photoBrowserPresentFromRect(imageIndexPath)
        
        transitionContext.containerView()?.addSubview(imgView)
        
        let toVc = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as! WHYPhotoBrowserViewController
        toVc.view.hidden = true 
        
        UIView.animateWithDuration(transitionDuration(transitionContext),
            animations: { () -> Void in
                
                imgView.frame = presentDelegate.photoBrowserPresentToRect(imageIndexPath)
                toView.alpha = 1.0
                
            }) { (_) -> Void in
                
                toVc.view.hidden = false
                imgView.removeFromSuperview()
                transitionContext.completeTransition(true)//通知系统动画结束，必须在动画结束时调用
                
        }
        
    }
    
    /// 解除转场动画
    ///
    /// - parameter transitonContext: 转场上下文
    private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let presentDelegate  = presentDelegate,
            dismissDelegate = dismissDelegate else {
                return
        }
        
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
        let imageView = dismissDelegate.imageViewForDismiss()
        let dismissIndexPath = dismissDelegate.indexPathForDismiss()
        
        transitionContext.containerView()?.addSubview(imageView)
        
        UIView.animateWithDuration(transitionDuration(transitionContext),
            animations: { () -> Void in
            
//                fromView.hidden = true
                fromView.alpha = 0
                imageView.frame = presentDelegate.photoBrowserPresentFromRect(dismissIndexPath)
            
            }) { (_) -> Void in
                
                imageView.removeFromSuperview()
                fromView.removeFromSuperview()
                transitionContext.completeTransition(true)
                
        }
    }

}
